package com.heima.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.crm.common.contents.CommonMessageContent;
import com.heima.crm.common.result.PageResult;
import com.heima.crm.common.result.Result;
import com.heima.crm.mapper.SysOperLogMapper;
import com.heima.crm.pojos.dtos.LogPageDto;
import com.heima.crm.pojos.entity.SysOperLog;
import com.heima.crm.service.SysOperLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class SysOperLogServiceImpl implements SysOperLogService {

    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    /**
     * 系统日志分页查询
     * @param logPageDto
     * @return
     */
    @Override
    public Result pageLog(LogPageDto logPageDto) {
        //参数校验
        if (logPageDto == null){
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        if (logPageDto.getPage() == null){
            logPageDto.setPage(1);
        }
        if (logPageDto.getSize()== null){
            logPageDto.setSize(10);
        }

        QueryWrapper<SysOperLog> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(logPageDto.getKeyword())){
            queryWrapper.like("oper_url",logPageDto.getKeyword());
        }

        if (logPageDto.getOperTime() != null){
            LocalDateTime start = logPageDto.getOperTime().atStartOfDay();
            //LocalDateTime start = LocalDateTime.of(logPageDto.getOperTime(), LocalTime.MIN);
            LocalDateTime end = logPageDto.getOperTime().atTime(LocalTime.MAX);
            //LocalDateTime end = LocalDateTime.of(logPageDto.getOperTime(),LocalTime.MAX);
            queryWrapper.ge("oper_time",start).le("oper_time",end);
        }

        Page page = new Page(logPageDto.getPage(), logPageDto.getSize());

        page = sysOperLogMapper.selectPage(page,queryWrapper.orderByDesc("oper_time"));

        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }


//    /**
//     * 详细日志查询
//     * @param id
//     * @return
//     */
//    @Override
//    public Result log(Long id) {
//        if (id == null){
//            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
//        }
//
//        SysOperLog sysOperLog = sysOperLogMapper.selectById(id);
//
//        return Result.success(sysOperLog);
//    }
}
